var url =  "/openx/logistics/partner/listPartner", formId = 'partnerform', param = {};
(function () {
    $.openx.initTable("table", url, null);
    $.openx.setTableToolBar('partnerbar'); //设置工具栏
})();

/**物流新增操作 */
$.openx.insert = function () {
    var innerHtml = $.openx.innerHtml('partnerformtpl');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['auto', 'auto'], '新增物流', html, false, function () {
            if ($.openx.validation(formId)) {
                var bean = $.openx.getJsonObj(formId);
                createOrUpdatePartner(bean, 'INSERT');
            }
        });
    })
};

/**
 *  修改物流操作
 */
$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
        var innerHtml = $.openx.innerHtml('partnerformtpl');
        laytpl(innerHtml).render(row[0], function (html) {
            $.openx.openLayer(['auto', 'auto'], '修改物流基本信息', html, false, function () {
                if ($.openx.validation(formId)) {
                    var bean = $.openx.getJsonObj(formId);
                    createOrUpdatePartner(bean, 'UPDATE');
                }
            });
        });
    });
};

function statusFormat(v, row) {
    var id = row.logisticsId + new Date().getTime();
    var html = '<div class="switch">';
    html += '<div class="onoffswitch">';
    if (row.status === 'ENABLE') {
        html += "<input type='checkbox'  checked class='onoffswitch-checkbox' onclick ='javascript:changeLogisticsStatus(" + JSON.stringify(row) + ")'  id='" + id + "'>";
    } else {
        html += "<input type='checkbox'  class='onoffswitch-checkbox' onclick ='javascript:changeLogisticsStatus(" + JSON.stringify(row) + ")'  id='" + id + "'>";
    }
    html += '<label class="onoffswitch-label" for="' + id + '">';
    html += '<span class="onoffswitch-inner"></span>';
    html += '<span class="onoffswitch-switch"></span>';
    html += '</label>';
    html += '</div>';
    html += '</div>';
    return html;
}

/**  改变物流状态 */
function changeLogisticsStatus(row) {
    $X("/logistics/partner", "enableOrDisablePartner").callx({
        logisticsId: row.logisticsId,
        onResult: function (data) {
            if (data) {
                $.openx.tableRefresh(null, null, true);
                layer.closeAll();
                layer.msg("操作成功");
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
}

/**
 * @param bean  入参对象bean       operationType  =    INSERT OR UPDATE
 */
function createOrUpdatePartner(bean, operationType) {
    $X("/logistics/partner", "createOrUpdatePartner").callx({
        bean: bean,
        operationType: operationType,
        onResult: function (data) {
            if (data) {
                $.openx.tableRefresh(null, null, true);
                layer.closeAll()
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
}


//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (row) {
        $X("/logistics/partner", "deletePartner").callx({
            logisticsId: row[0].logisticsId,
            onResult: function (data) {
                $.openx.tableRefresh(null, null, true);
                layer.msg("删除成功");
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};
